import { Color, Label, Layers, Node, Overflow, Prefab, UITransform, Vec2, Vec3, instantiate, tween, v2, v3 } from 'cc';
import ResManager from './ResManager';
import { PrefabEnum } from './PrefabEnum';
import { UIManager } from './UIManager';
export class Utils {

    static Red: Color = new Color('#B32121')

    // src/index.ts
    static bigNumberTransform(value) {
        if (value < 10000) {
            value = value.toFixed()
        }
        if (value >= 10000 && value < 10000000) {
            value = (value / 10000).toFixed(2) + '万';
        }
        if (value >= 10000000 && value < 10000000000) {
            value = (value / 10000000).toFixed(2) + '千万';
        }
        if (value >= 10000000000 && value < 1000000000000) {
            value = (value / 10000000000).toFixed(2) + '百亿';
        }
        if (value >= 1000000000000 && value < 100000000000000) {
            value = (value / 1000000000000).toFixed(2) + '兆'; //万亿
        }
        if (value >= 100000000000000) {
            value = (value / 100000000000000).toFixed(2) + '百兆'; //百万亿，
        }
        return value;
    }

    static keepTwo(num) {
        return Math.floor(num * 100) / 100
    }


    //计算两个整数之间的百分比
    //curNum：当前数据，totalNum：总数据，isHasPercentStr：是否返回%字符
    static getPercent(curNum, totalNum) {
        var isHasPercentStr = false
        curNum = parseFloat(curNum);
        totalNum = parseFloat(totalNum);

        if (isNaN(curNum) || isNaN(totalNum)) {
            return '-';
        }
        var value = isHasPercentStr ? totalNum <= 0 ? '0%' : (Math.round(curNum / totalNum * 10000) / 100.00 + '%') : totalNum <= 0 ? 0 : (Math.round(curNum / totalNum * 10000) / 100.00);
        if (value > "0" && value <= "10") {
            return 0.1
        } else if (value > "10" && value <= "20") {
            return 0.2
        } else if (value > "20" && value <= "30") {
            return 0.3
        } else if (value > "30" && value <= "40") {
            return 0.4
        } else if (value > "40" && value <= "50") {
            return 0.5
        } else if (value > "50" && value <= "60") {
            return 0.6
        } else if (value > "60" && value <= "70") {
            return 0.7
        } else if (value > "70" && value <= "80") {
            return 0.8
        } else if (value > "80" && value <= "90") {
            return 0.9
        } else if (value > "90" && value <= "100") {
            return 1
        }
    }

    static formatDate(date, fmt?) {
        if (typeof date == 'string') {
            return date;
        }

        if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";

        if (!date || date == null) return null;


        let year = date.getFullYear()
        let month = date.getMonth() + 1
        month = month < 10 ? '0' + month : month

        let d = date.getDate()
        d = d < 10 ? '0' + d : d
        let hour = date.getHours()
        hour = hour < 10 ? '0' + hour : hour
        let min = date.getMinutes()
        min = min < 10 ? '0' + min : min
        let sec = date.getSeconds()
        sec = sec < 10 ? '0' + sec : sec

        fmt = fmt.replace('yyyy', year)
        fmt = fmt.replace('MM', month)
        fmt = fmt.replace('dd', d)
        fmt = fmt.replace('hh', hour)
        fmt = fmt.replace('mm', min)
        fmt = fmt.replace('ss', sec)

        return fmt
    }

    static secondToStr(time) {
        time = Math.floor(time)
        if (time <= 0) {
            return '00:00:00'
        }

        var hours = Math.floor(time / 3600);
        var min = Math.floor((time % 3600) / 60);
        var sec = time % 60;

        hours = hours < 10 ? '0' + hours : hours
        min = min < 10 ? '0' + min : min
        sec = sec < 10 ? '0' + sec : sec

        return hours + ":" + min + ":" + sec
    }


    static showToPosition(node: Node, pos?: Vec2) {
        node.active = true
        if (!pos) {
            pos = v2(node.position.x, node.position.y)
        }
        node.setPosition(v3(0, 0, 0))
        tween().target(node).to(0.2, { position: v3(pos.x, pos.y, 0) }).start()
    }

    static async addButtonTip(btn: Node, msg: string, isUI: boolean = false) {
        // UIManager.instance.tip(msg)
        // return
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.ButtonTip)

        let tipNode = instantiate(prefab)
        tipNode.getChildByName('Label').getComponent(Label).string = msg
        btn.addChild(tipNode)
        // if(isUI){
        //     tipNode.layer = Layers.Enum.UI_2D

        //     for(let ch of tipNode.children){
        //         ch.layer = Layers.Enum.UI_2D
        //     }
        // }
    }

    static removeButtonTip(btn: Node) {
        let name = PrefabEnum.ButtonTip.split('/')[1]
        let tip = btn.getChildByName(name)
        tip?.destroy()
    }

    static handleEllipsis(labelNode: Node) {
        
        let node = instantiate(labelNode)
        let label = node.getComponent(Label)
        label.overflow = Overflow.NONE
        let string = labelNode.getComponent(Label).string
        label.string = string
        labelNode.addChild(node)
        node.position = v3(1000, 100, 0)
        label.updateRenderData()
       
        let width = labelNode.getComponent(UITransform).width
        if (node.getComponent(UITransform).width > width) {
            while (true) {
                string = string.substring(0, string.length - 1)
                label.string = string + '...'
                label.updateRenderData()
                if (node.getComponent(UITransform).width <= width) {
                    string += '...'
                    node.destroy()
                    break
                }
            }
        }

        labelNode.getComponent(Label).string = string

        // let label = labelNode.getComponent(Label)

        // let width = labelNode.getComponent(UITransform).width
        // if(width > maxWidth){
        //     let str = label.string.replace('...', '')
        //     str = str.substring(0, str.length - 1)
        //     label.string = str + '...'
        // }


    }
}


